/**
 * 
 * 
 * Created on 25 jun. 2011 @ 21:41:00 by Laurens Andriessen
 */

package combat.dudes.client.gui.threads.map.main;

import gnu.cajo.utils.extra.TransparentItemProxy;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import combat.dudes.DudesConfig;
import combat.dudes.DudesFunctions;
import combat.dudes.client.ClientApp;
import combat.dudes.client.ClientFunctions;
import combat.dudes.client.gui.images.Painter;
import combat.dudes.client.gui.threads.map.ClientGUIMap;
import combat.dudes.client.gui.threads.map.ClientGUIMap.MapType;
import combat.dudes.client.gui.threads.shell.GUIShell;
import combat.dudes.protocol.interfaces.OnboardWeaponSystemInt;


/**
 * 
 * @author Laurens Andriessen
 * @version 1.0
 */
public class GUIMainMap {

	private final static JPanel myPanel = GUIShell.windowWorldView.myBodyPanel;
	public final static int mapWidth = myPanel.getWidth();
	public final static int mapHeight = myPanel.getHeight();

	public GUIMainMap() {

		myPanel.add(new ClientGUIMap(MapType.MAIN, mapWidth, mapHeight));

		myPanel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent m) {
				if(ClientApp.SharedVars.getMyVehicle() != null)
					if(SwingUtilities.isLeftMouseButton(m))
						ClientApp.SharedVars.getMyVehicle().orderMove(ClientApp.SharedVars.getMyVehicle().getOrders().size(), ClientFunctions.GUI.convertMapToPoint(MapType.MAIN, m.getPoint()),
								DudesConfig.SharedVars.getMaxSpeed(ClientApp.SharedVars.getMyVehicle().getVehicleType()));

				if(SwingUtilities.isRightMouseButton(m)) {
					/* OnboardWeaponSystemInt weaponsys = null;
					 * for(Object sysRemote : ClientApp.SharedVars.getMyVehicle().getOnboardSystems_Remote()) {
					 * OnboardSystemInt sys = (OnboardSystemInt) TransparentItemProxy.getItem(sysRemote, new Class[] { OnboardSystemInt.class });
					 * if(sys != null)
					 * if(sys.getOnboardSystemType() == OnboardSystemType.WEAPON)
					 * weaponsys = (OnboardWeaponSystemInt) sys;
					 * }
					 * if(weaponsys != null) */


					OnboardWeaponSystemInt sys = (OnboardWeaponSystemInt) TransparentItemProxy.getItem(ClientApp.SharedVars.getMyVehicle().getOnboardSystems_Remote().get(0),
							new Class[] { OnboardWeaponSystemInt.class });
					System.out.println("Ammo: " + sys.getAmmo());
					ClientApp.SharedVars.getMyVehicle().orderAttack(
							ClientApp.SharedVars.getMyVehicle().getOrders().size(),
							ClientFunctions.GUI.convertMapToPoint(MapType.MAIN, m.getPoint()),
							sys);
				}

			}
		});

		int i = 0;
		while(true) {
			i++;

			long start = DudesFunctions.DateTime.getTimeInMs();

			myPanel.setBounds(0, 0, mapWidth + (i % 2), mapHeight + (i % 2));
			Painter.repaint(myPanel);

			DudesFunctions.Status.DelayUpdate(start, 80, Thread.currentThread());
		}
	}

}
